package jam.UserInterface.UI;
import javax.swing.*;
import jam.network.message.*;
import java.io.File;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.imageio.ImageIO;

public class registrationWindow extends JFrame implements ActionListener, MouseListener, KeyListener, FocusListener{	
	
	static JFrame registration = new JFrame();
	JButton SUBMIT, CANCEL;
	JPanel panelLeft, panelRight, panelBottom,panelTop, panelUser, panelEmail, panelConfE, panelConfirmE, panelName, panelNick,  panelPword, panelPassword, panelConfP, panelConfPword;
	JLabel intro, email, confirmE, displayName, pword, confirmP;
	final JTextField  username, confirmU,nickname;
	final JPasswordField  password, confirmPword;
	
	
	public registrationWindow(){
		//optional look and feel
		UIManager.LookAndFeelInfo[] lfi= UIManager.getInstalledLookAndFeels();
		    
		try
		{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception e)
		{
		System.out.println("Can't set chosen look-and-feel!");
	    }

		registration.setDefaultLookAndFeelDecorated(true);
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		registration.setBounds(((int)(screenSize.getWidth()-600))/2,((int)(screenSize.getHeight()-320))/2,600,320);
		registration.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		//change the icon code
		String currentDirectory = System.getProperty("user.dir");
		try {
			Image image = ImageIO.read(new File(currentDirectory + "/src/jam/UserInterface/Icons/JAMicon.png"));
			registration.setIconImage(image);
		}catch (Exception e){System.out.println("The file could not be read!!!!");}
		
		intro = new JLabel();
		intro.setText("Please fill in the form below to register:-");

		panelTop=new JPanel(new FlowLayout());
		panelTop.add(intro);

		email = new JLabel();
		email.setText("Your email:");
		final String init = "Username";
		username = new JTextField(init,30);
		username.setForeground(Color.GRAY);
		username.addMouseListener(this);
		username.addKeyListener(this);

		panelEmail = new JPanel(new FlowLayout());
		panelEmail.add(email);
		panelUser = new JPanel(new FlowLayout());
		panelUser.add(username);

		confirmE = new JLabel();
		confirmE.setText("Confirm your email:");
		
		final String inU="Confirm email";
		confirmU = new JTextField(inU,30);
		confirmU.setForeground(Color.GRAY);
		confirmU.addMouseListener(this);
		confirmU.addKeyListener(this);
		confirmU.addFocusListener(this);


		panelConfirmE= new JPanel(new FlowLayout());
		panelConfirmE.add(confirmU);
		panelConfE = new JPanel(new FlowLayout());
		panelConfE.add(confirmE);

		displayName = new JLabel();
		displayName.setText("Display name:");
		
		final String inNick="Nickname";
		nickname = new JTextField(inNick,30);
		nickname.setForeground(Color.GRAY);
		nickname.addMouseListener(this);
		nickname.addKeyListener(this);
		nickname.addFocusListener(this);

		panelNick= new JPanel(new FlowLayout());
		panelNick.add(nickname);
		panelName =new JPanel(new FlowLayout());
		panelName.add(displayName);

		pword = new JLabel();
		pword.setText("Password:");
		password = new JPasswordField(30);

		panelPword =new JPanel(new FlowLayout());
		panelPword.add(pword);
		panelPassword= new JPanel(new FlowLayout());
		panelPassword.add(password);

		confirmP = new JLabel();
		confirmP.setText("Confirm password:");
		confirmPword = new JPasswordField(30);

		panelConfP = new JPanel(new FlowLayout());
		panelConfP.add(confirmP);
		panelConfPword = new JPanel(new FlowLayout());
		panelConfPword.add(confirmPword);


		SUBMIT = new JButton("SUBMIT");
		SUBMIT.addActionListener(this);
		CANCEL = new JButton("CANCEL");
		CANCEL.addActionListener(this);


		panelLeft=new JPanel(new GridLayout(5,1));
		panelLeft.add(panelEmail);
		panelLeft.add(panelConfE);	  
		panelLeft.add(panelName);	 
		panelLeft.add(panelPword);	 
		panelLeft.add(panelConfP);	  


		panelRight = new JPanel(new GridLayout(5,2));
		
		panelRight.add(panelUser);
		panelRight.add(panelConfirmE);	  
		panelRight.add(panelNick);	 
		panelRight.add(panelPassword);	 
		panelRight.add(panelConfPword);	  

		panelBottom = new JPanel(new FlowLayout());
		panelBottom.add(CANCEL);
		panelBottom.add(SUBMIT);

		registration.add(panelTop, BorderLayout.NORTH);
		registration.add(panelLeft,BorderLayout.WEST);
		registration.add(panelRight,BorderLayout.CENTER);
		registration.add(panelBottom,BorderLayout.SOUTH);	  
		registration.setTitle("JAM - Register");
		
		registration.setVisible(true);
	}

	public void actionPerformed(ActionEvent e){
		
		if (e.getSource() == SUBMIT){
			String usernameValue=username.getText(); //disp
			String confUserValue=confirmU.getText();
			String nicknameValue=nickname.getText(); //disp
			char[] passwordValue=password.getPassword();
			char[] confPassValue=confirmPword.getPassword();
			
			int top=usernameValue.indexOf("@");
			int bot=usernameValue.lastIndexOf(".");		
			boolean un = top > 1 && bot > top + 3 && (bot + 3 == usernameValue.length() || bot + 4 == usernameValue.length());
	
			boolean passwordEquality = Arrays.equals(passwordValue,confPassValue);
			System.out.println("blnResult: " +passwordEquality);
	
	
			if (un && usernameValue.equals(confUserValue) && passwordValue.length>0 && passwordEquality) {
				registration.dispose();
				ClientActionRegister register = new ClientActionRegister(username.getText(), password.getPassword().toString());
			}
			else{
				JOptionPane.showMessageDialog(this,"Incorrect email OR email or password mismatch",
						"Error",JOptionPane.ERROR_MESSAGE);
			}
		}
	
		if (e.getSource() == CANCEL){
			SwingUtilities.invokeLater(new Runnable(){
				public void run(){
					new loginWindow(); // creates the user interface
				}
			});
			registration.dispose();
		}
	
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == username && e.getKeyCode() == KeyEvent.VK_TAB){
			System.out.println(e.getKeyChar());
			System.out.println(KeyEvent.VK_ENTER);
			confirmU.setText("");
			confirmU.setForeground(Color.BLACK);
			confirmU.setCaretPosition(0);
		}
		else if (e.getSource() == confirmU && e.getKeyCode() == KeyEvent.VK_TAB){
			nickname.setText("");
			nickname.setForeground(Color.BLACK);
			nickname.setCaretPosition(0);
		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == username && username.getText().equals("Username")){
			username.setText("");
			username.setForeground(Color.BLACK);
		
		}
		else if (e.getSource() == confirmU && confirmU.getText().equals("Confirm email")){
			confirmU.setText("");
			confirmU.setForeground(Color.BLACK);
		}
		else if(e.getSource() == nickname && nickname.getText().equals("Nickname")){
			nickname.setText("");
			nickname.setForeground(Color.BLACK);
		}
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void focusGained(FocusEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == confirmU){
			confirmU.setText("");
			confirmU.setForeground(Color.BLACK);
		}
		else if (e.getSource() == nickname){
			nickname.setText("");
			nickname.setForeground(Color.BLACK);
		}
		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
